package ui;
import java.awt.event.ActionEvent;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import dao.*;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class searchWindow extends javax.swing.JFrame {
	private JPanel jPanel1;
	private JTextField searchScientificNameField;
	private JButton searchOkButton;
	private AbstractAction abstractAction1;
	private JButton cancel;
	private AbstractAction asearchCloseAction;
	private JTextField searchPopularNameField;
	private JLabel jLabel2;
	private JLabel jLabel1;
	
	public searchWindow() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		Vector<String> hlist = new Vector<String>();
		
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Search");
			{
				jPanel1 = new JPanel();
				GroupLayout jPanel1Layout = new GroupLayout((JComponent)jPanel1);
				jPanel1.setLayout(jPanel1Layout);
				{
					jLabel1 = new JLabel();
					jLabel1.setText("Scientific Name:");
				}
				{
					searchOkButton = new JButton();
					searchOkButton.setText("OK");
					searchOkButton.setAction(getAsearchCloseAction());
				}
				{
					jLabel2 = new JLabel();
					jLabel2.setText("Popular Name:");
				}
				{
					searchScientificNameField = new JTextField();
				}
				{
					searchPopularNameField = new JTextField();
				}
				jPanel1Layout.setVerticalGroup(jPanel1Layout.createSequentialGroup()
					.addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(searchScientificNameField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(jLabel1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(searchPopularNameField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(jLabel2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(50)
					.addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(getCancel(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(searchOkButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(44, 44));
				jPanel1Layout.setHorizontalGroup(jPanel1Layout.createSequentialGroup()
					.addGap(12, 12, 12)
					.addGroup(jPanel1Layout.createParallelGroup()
					    .addComponent(jLabel2, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
					    .addComponent(jLabel1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addGroup(jPanel1Layout.createParallelGroup()
					    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					        .addComponent(searchScientificNameField, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 37, Short.MAX_VALUE))
					    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					        .addComponent(searchPopularNameField, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 37, Short.MAX_VALUE))
					    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					        .addGap(46)
					        .addComponent(searchOkButton, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
					        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					        .addComponent(getCancel(), GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE)))
					.addContainerGap());
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addComponent(jPanel1, 0, 270, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addComponent(jPanel1, 0, 390, Short.MAX_VALUE));
			pack();
			this.setSize(400, 175);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private AbstractAction getAsearchCloseAction() {
		if(asearchCloseAction == null) {
			asearchCloseAction = new AbstractAction("Search", null) {
				public void actionPerformed(ActionEvent evt) {
					BirdVO b = null, temp = null;
					Vector<BirdVO> bl = new Vector<BirdVO>();
					boolean flag = true, filled = false;
					String boom = searchScientificNameField.getText();
					String boom2 = searchPopularNameField.getText();
					
					//Check if any of the two fields are filled
					//TODO: filter by scientific name, if field is filled.
					try {
						if ( boom.length() > 0 ) {
							b = BirdDAO.findByScientificName(boom);
							filled = true;
						} 
					
						if ( boom2.length() > 0 ) {
							filled = true;
							for(BirdVO o: BirdDAO.findsAllBirdsByPopularName(boom2)) {
								temp = (BirdVO)o;
								bl.add(temp);
								
								// Does not allow duplicate entries
								if ( b != null && temp.getScientific_name() == b.getScientific_name() ) {
									flag = false;
								}
							}
						}
						
						if ( flag && b != null )
							bl.add(b);
					
					} catch (Throwable e) {
						System.out.println(e.getMessage());
						filled = true;
					};
					
					BirdVO[] blist = new BirdVO[bl.size()];
					
					//Stupid java does not allow explicit cast from arrays to
					//vector. Gotta manually iterate over each element and cast it.
					if ( bl.size() > 0 ) {
						blist = new BirdVO[bl.size()];
						
						for(int i = 0; i < bl.size(); i++) {
							blist[i] = (BirdVO)bl.elementAt(i);
						}
					}
					
					if ( blist.length > 0 ) {
					
						// Call the search form and pass over the data.
						resultWindow rW = new resultWindow();
						rW.setLocationRelativeTo(null);
						rW.setVisible(true);
						rW.insertData(blist);
					
					} else {
						successDialog sD = new successDialog(searchWindow.this);
						sD.pack();
						sD.setLocationRelativeTo(null);
						sD.setVisible(true);
						
						if ( !filled ) {
							//No field was filled. Can't search.
							sD.setLabelText("No fields have been filled.");
						} else {
							//No entries have been found.
							sD.setLabelText("No matching bird has been found.");
						}
					}
				}
			};
		}
		return asearchCloseAction;
	}
	
	private JButton getCancel() {
		if(cancel == null) {
			cancel = new JButton();
			cancel.setText("Cancel");
			cancel.setAction(getAbstractAction1());
		}
		return cancel;
	}
	
	private AbstractAction getAbstractAction1() {
		if(abstractAction1 == null) {
			abstractAction1 = new AbstractAction("Cancel", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return abstractAction1;
	}

}
